package test;

import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

class DifferentColumnCursors extends JFrame
{
  DifferentColumnCursors (String title)
  {
   // Pass the title to the JFrame superclass so that it appears in
   // the title bar.

   super (title);

   // Tell the program to exit when the user either selects Close
   // from the System menu or presses an appropriate X button on the
   // title bar.

   setDefaultCloseOperation (EXIT_ON_CLOSE);

   // Create a default table model consisting of headersText columns
   // and 10 rows.

   String [] headers = { "First", "Second" };

   DefaultTableModel dtm = new DefaultTableModel (headers, 5);

   // Populate all cells in the default table model with integer
   // values.

   int nrows = dtm.getRowCount ();
   int ncols = dtm.getColumnCount ();
   int n = 0;

   for (int i = 0; i < nrows; i++)
      for (int j = 0; j < ncols; j++)
        dtm.setValueAt (new Integer (n++), i, j);

   // Create a table using the previously created default table
   // model.
   // The jt variable is final, so it can be accessed from within an 
   // inner class.

   final JTable jt = new JTable (dtm);

   // Set the preferred size of the table's scrollable viewport.
   // That is an alternative to specifying setSize() for the frame 
   // window.
 
   jt.setPreferredScrollableViewportSize (new Dimension (250, 125));

   // Create an object from an anonymous subclass of the
   // MouseMotionAdapter class. Within that subclass, override the
   // mouseMoved() method.

   MouseMotionAdapter mma;
   mma = new MouseMotionAdapter ()
      {
        public void mouseMoved (MouseEvent e)
        {
          // Return the pixel position of the mouse cursor
          // hotspot.

          Point p = e.getPoint ();

          // Convert the pixel position to the zero-based
          // column index of the table column over which the
          // mouse cursor hotspot is located. The result is a
          // view-based column index. If that index refers to
          // the leftmost column, display a crosshair cursor.
          // Otherwise, display a hand cursor.

          if (jt.columnAtPoint (p) == 0)
            jt.setCursor (Cursor.getPredefinedCursor
                      (Cursor.CROSSHAIR_CURSOR));
          else
            jt.setCursor (Cursor.getPredefinedCursor
                      (Cursor.HAND_CURSOR));
        }
      };

   // Register the previous MouseMotionAdapter object as a listener
   // to mouse movement events originating from the table.

   jt.addMouseMotionListener (mma);

   // Place the table in a JScrollPane object (to allow the table to
   // be vertically scrolled and display scrollbars, as necessary).

   JScrollPane jsp = new JScrollPane (jt);

   // Add the JScrollPane object to the frame window's content pane.
   // That allows the table to be displayed within a displayed
   // scroll pane.

   getContentPane ().add (jsp);

   // Size the frame window to the preferred size of its scroll pane
   // container.

   pack ();

   // Display the frame window and all contained
   // components/containers.

   setVisible (true);
  }

  public static void main (String [] args)
  {
   // Create a DifferentColumnCursors object, which creates the GUI.

   new DifferentColumnCursors ("Different Column Cursors");
  }
}